using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_ctrl_dashboard : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ImageProvider ip = new ImageProvider();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		bool letView = false;
		bool isLoggedIn = false;
		string imgtitle = "";

		//if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
		if(Session.Count > 0 && Session["ZapprUserId"] != null) {
			isLoggedIn = true;
		}

		/*
		 * images from a user 
		 */
		if(isLoggedIn) {
			//Account acc = ap.GetAccountBySession(Session["Zappr"].ToString());
			Account acc = ap.GetAccount(Session["ZapprUserId"].ToString());
			ArrayList items = ip.GetImageList(acc.ID, 0, 6, 0);

			this.plcOnlineUsers.Visible = true;

			List<Account> onlineusers = this.ap.GetAllOnlineUser();
			this.lblOnline.Text = "&raquo; Online Users: " + onlineusers.Count.ToString();

			if(items.Count > 0) {
				plcOwnImages.Visible = true;
				plcOwnEmpty.Visible = false;
			}
			else {
				plcOwnImages.Visible = false;
				plcOwnEmpty.Visible = true;

				HyperLink upload = new HyperLink();
				upload.CssClass = "editBoxTitle";
				upload.Text = "&raquo;" + GetLocalResourceObject("UploadNow").ToString();
				upload.NavigateUrl = "~/upload/default.aspx";

				plcOwnEmpty.Controls.Add(upload);
				plcOwnEmpty.Controls.Add(new LiteralControl("<br /><br />"));
			}

			// avatar
			HyperLink av = new HyperLink();
			av.CssClass = "avatarImg";
			av.NavigateUrl = "~/images/" + acc.Username + "/default.aspx";

			if(acc.Avatar != "" && acc.Avatar != null) {
				av.ImageUrl = "~/Avatar/" + acc.Avatar;
			}
			else {
				av.ImageUrl = "~/App_Themes/Default/Images/avatar_placeholder.jpg";
			}

			userAvatar.Controls.Add(av);
			userAvatar.Controls.Add(new LiteralControl(""));

			Label litSlashOne = new Label();
			litSlashOne.CssClass = "txtSmallGreen";
			litSlashOne.Text = GetLocalResourceObject("Hello").ToString();
			litSlashOne.Text += " ";
			litSlashOne.Text += acc.Username;

			userText.Controls.Add(litSlashOne);

			linkOwn.NavigateUrl = "~/images/" + acc.Username + "/default.aspx";

			foreach(ImageItem item in items) {
				// check privacy
				if(isLoggedIn) {
					if(item.Access > 0) {
						// this time: only the user itself
						// next time: friends and family
						if(item.UserID == acc.ID) {
							letView = true;
						}
					}
					else {
						letView = true;
					}
				}
				else {
					if(item.Access == 0) {
						letView = true;
					}
				}

				if(letView) {
					HyperLink img = new HyperLink();
					img.ImageUrl = "~/Images/" + item.Image75x75;
					img.CssClass = "listSquareThumbnail";
					img.NavigateUrl = "~/photo/" + item.ImageID.ToString() + "/default.aspx";

					plcOwnImageList.Controls.Add(img);

					letView = false;
				}
			}
		}
		else{
			userAvatar.Visible = false;
			userText.Visible = false;
			userBreak.Visible = false;
			plcOwnImages.Visible = false;
		}

		/*
		 * random image
		 */

		ImageItem rand = ip.GetRandomImage();

		imgtitle = rand.Name;

		if(imgtitle.Length > 22) {
			if(imgtitle.IndexOf(' ') == -1 || imgtitle.IndexOf(' ') > 21) {
				imgtitle = rand.Name.Substring(0, 22) + "...";
			}
		}

		imgView.ImageUrl = "~/Images/" + rand.Image500;
		imgView.NavigateUrl = "~/photo/" + rand.ImageID.ToString() + "/default.aspx";

		/*
		 * images from all users
		 */
		ArrayList list = ip.GetNewestImageList(6);

		foreach(ImageItem item in list) {
			HyperLink img = new HyperLink();
			img.ImageUrl = "~/Images/" + item.Image75x75;
			img.CssClass = "listSquareThumbnail";
			//img.NavigateUrl = "~/default.aspx?item=" + item.ImageID.ToString();
			img.NavigateUrl = "~/photo/" + item.ImageID.ToString() + "/default.aspx";

			plcAllImagesList.Controls.Add(img);
		}
	}

	/// <summary>
	/// Clickit
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Clickit(object sender, EventArgs e) {
		ImageItem rand = ip.GetRandomImage();

		string imgtitle = rand.Name;

		if(imgtitle.Length > 22) {
			if(imgtitle.IndexOf(' ') == -1 || imgtitle.IndexOf(' ') > 21) {
				imgtitle = rand.Name.Substring(0, 22) + "...";
			}
		}

		linkRandom.Text = imgtitle;
		linkRandom.NavigateUrl = "~/photo/" + rand.ImageID.ToString() + "/default.aspx";

		imgView.ImageUrl = "~/Images/" + rand.Image500;
		imgView.NavigateUrl = "~/photo/" + rand.ImageID.ToString() + "/default.aspx";
	}
}
